package com.best.intelligentTrolley.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;


import com.best.intelligentTrolley.R;

import java.util.ArrayList;
import java.util.List;

public class TextBanner extends ViewGroup {
    private List<String> mData = new ArrayList<>();
    private ViewFlipper viewFlipper;
    private int parentWidthSpec;

    public TextBanner(Context context) {
        super(context);
    }

    public TextBanner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextBanner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {


        int top = 0;
        int bottom = getChildAt(0).getMeasuredHeight();


        int left = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            left = (parentWidthSpec - view.getMeasuredWidth()) / 2;
            view.layout(left, top, left + view.getMeasuredWidth(), bottom);
            top += view.getMeasuredHeight();
            bottom = top + view.getMeasuredHeight();

        }
        Log.d("tzg", "bottom: " + bottom);
        Log.d("tzg", "top: " + top);


    }


    public void setData(List<String> data) {
        mData.clear();
        if (data.isEmpty()) {
            return;
        }
        this.mData = data;

        setTextList();
    }

    private List<TextView> textViews = new ArrayList<>();

    private void setTextList() {
        viewFlipper = (ViewFlipper) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_viewflip, this, false);
        for (String mDatum : mData) {

            TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_textview, this, false);
            view.setText(mDatum);
            textViews.add(view);
            viewFlipper.addView(view);

        }
        viewFlipper.setInAnimation(getContext(), R.anim.come_in);
        viewFlipper.setOutAnimation(getContext(), R.anim.come_out);
        viewFlipper.setFlipInterval(2000);
        addView(viewFlipper);
    }

    public void refreshData(List<String> data){
        for(int i=0;i<textViews.size();i++){
            textViews.get(i).setText(data.get(i));
        }
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        parentWidthSpec = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeightSpec = MeasureSpec.getSize(heightMeasureSpec);


        int childWidth = MeasureSpec.makeMeasureSpec(parentWidthSpec, MeasureSpec.AT_MOST);
        int childHeight = MeasureSpec.makeMeasureSpec(parentHeightSpec, MeasureSpec.AT_MOST);

        int totalHeight = getChildAt(0).getMeasuredHeight();

        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            measureChild(view, childWidth, childHeight);
        }
        Log.d("tzg", "totalCount: " + totalHeight);
        setMeasuredDimension(parentWidthSpec, totalHeight);

    }


    public void startAnimation() {
        // 1、设置幻灯片的形式滚动
        // viewFlipper.startFlipping();

        // 2、设置自动翻页滚动
        viewFlipper.setAutoStart(true);
        viewFlipper.isAutoStart();
    }
}
